package com.gxan.service.impl;

import com.gxan.common.Constants;
import com.gxan.common.JsonResult;
import com.gxan.common.R;
import com.gxan.dao.HumanDao;
import com.gxan.dao.impl.HumanDaoImpl;
import com.gxan.pojo.Human;
import com.gxan.service.HumanService;

import java.sql.SQLException;
import java.util.List;

public class HumanServiceImpl implements HumanService {
    HumanDao humanDao = new HumanDaoImpl();

    @Override
    public R addHuman(Human human) {
        R r = new R();
        try {
            int result = humanDao.saveHuman(human);
            if (result > 0) {//成功
                r.setCode(200);
                r.setMsg("添加员工成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult queryHumanList(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            jsonResult.setCode(0);
            jsonResult.setMsg("查询成功");
            //统计表中所有的数据
            //1.总条数
            Integer totalSize = humanDao.countHuman();
            jsonResult.setCount(totalSize);
            //2.表中所有的数据，封装到List
            Integer param01 = (currentPage - 1) * pageSize;
            Integer param02 = pageSize;
            List<Human> humanList = humanDao.queryHumanList(param01, param02);
            jsonResult.setData(humanList);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult reviewQueryHumanList(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            //统计表中所有的数据
            //1.总条数
            Integer totalSize = humanDao.reviewCountHuman();
            jsonResult.setCount(totalSize);
            if (totalSize > 0) {
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                //2.表中所有的数据，封装到List
                Integer param01 = (currentPage - 1) * pageSize;
                Integer param02 = pageSize;
                List<Human> humanList = humanDao.reviewQueryHumanList(param01, param02);
                jsonResult.setData(humanList);
            } else {
                jsonResult.setCode(404);
                jsonResult.setMsg("暂无需要复核的档案");
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public JsonResult reviewQueryHumanList(Integer currentPage, Integer pageSize, String human_name) {
        JsonResult jsonResult = new JsonResult();
        try {
            //统计表中所有的数据
            //1.总条数
            Integer totalSize = humanDao.reviewSearchCountHuman(human_name);
            jsonResult.setCount(totalSize);
            if (totalSize > 0) {
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                //2.表中所有的数据，封装到List
                Integer param01 = (currentPage - 1) * pageSize;
                Integer param02 = pageSize;
                List<Human> humanList = humanDao.reviewQueryHumanList(param01, param02, human_name);
                jsonResult.setData(humanList);
            } else {
                jsonResult.setCode(404);
                jsonResult.setMsg("没有该姓名的档案");
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R updateHuman(Human human) {
        R r = new R();
        try {
            int result = humanDao.updateHuman(human);
            if (result > 0) {//成功
                r.setCode(200);
                r.setMsg("修改员工成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }

    @Override
    public R delHumanById(String human_id) {
        R r = new R();
        //假删除
        try {
            int result = humanDao.delHumanById(human_id, Constants.STATUS_DEL);
            if (result > 0) {
                r.setCode(200);
                r.setMsg("操作成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }

        return r;
    }

    @Override
    public R delHumanById(String[] ids) {
        R r = new R();
        //假删除
        try {
            if (ids.length > 0) {
                for (String id : ids) {
                    humanDao.delHumanById(id, Constants.STATUS_DEL);
                }
            }
            r.setCode(200);
            r.setMsg("操作成功");
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }

        return r;
    }

    @Override
    public JsonResult queryHumanList(Integer currentPage, Integer pageSize, String human_name) {
        JsonResult jsonResult = new JsonResult();
        try {

            //统计表中所有的数据
            //1.总条数
            Integer totalSize = humanDao.countHuman(human_name);
            if (totalSize > 0) {
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(totalSize);
                //2.表中所有的数据，封装到List
                Integer param01 = (currentPage - 1) * pageSize;
                Integer param02 = pageSize;
                List<Human> humanList = humanDao.queryEmployeeList(param01, param02, human_name);
                jsonResult.setData(humanList);
            } else {
                jsonResult.setCount(totalSize);
                jsonResult.setCode(404);
                jsonResult.setMsg("没有查到改姓名的相关数据");
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

    @Override
    public R toCheck(String human_id) {
        R r = new R();
        //提交审核，档案状态修改为审核中状态STATUS_REVIEWING
        try {
            int result = humanDao.changeCheckStatus(human_id, Constants.STATUS_REVIEWING);
            if (result > 0) {//成功
                r.setCode(200);
                r.setMsg("修改员工成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }

    @Override
    public R passCheck(String human_id) {
        R r = new R();
        //通过审核，档案状态修改为通过审核状态STATUS_REVIEWED
        try {
            int result = humanDao.changeCheckStatus(human_id, Constants.STATUS_REVIEWED);
            if (result > 0) {//成功
                r.setCode(200);
                r.setMsg("修改员工成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }

    @Override
    public R batchPass(String[] ids) {
        R r = new R();
        //批量通过审核，档案状态都修改为通过审核状态STATUS_REVIEWED
        try {
            if (ids.length > 0) {
                for (String id : ids) {
                    humanDao.changeCheckStatus(id, Constants.STATUS_REVIEWED);
                }
            }
            r.setCode(200);
            r.setMsg("操作成功");
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }

        return r;
    }

    @Override
    public R backCheck(String human_id) {
        //驳回档案，状态修改为起草状态STATUS_DRAFT
        R r = new R();
        try {
            int result = humanDao.changeCheckStatus(human_id, Constants.STATUS_DRAFT);
            if (result > 0) {//成功
                r.setCode(200);
                r.setMsg("修改员工成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }
}
